{{>partial_header}}
package {{packageName}}

import (
	"net/http"
{{#routers}}
	{{#mux}}
	"github.com/gorilla/mux"
	{{#featureCORS}}
	"github.com/gorilla/handlers"
	{{/featureCORS}}
	{{/mux}}
	{{#chi}}
	"github.com/go-chi/chi/v5"
	{{#featureCORS}}
	"github.com/go-chi/cors"
	{{/featureCORS}}
	{{/chi}}
{{/routers}}
)

// A Route defines the parameters for an api endpoint
type Route struct {
	Name        string
	Method	    string
	Pattern	    string
	HandlerFunc http.HandlerFunc
}

// Routes is a map of defined api endpoints
type Routes map[string]Route

// Router defines the required methods for retrieving api routes
type Router interface {
	Routes() Routes
	OrderedRoutes() []Route
}

// NewRouter creates a new router for any number of api routers
func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}}chi.Router{{/chi}}{{/routers}} {
{{#routers}}
	{{#mux}}
	router := mux.NewRouter().StrictSlash(true)
	{{/mux}}
	{{#chi}}
	router := chi.NewRouter()
	router.Use(Logger)
	{{#featureCORS}}
	router.Use(cors.Handler(cors.Options{}))
	{{/featureCORS}}
	{{/chi}}
{{/routers}}
	for _, api := range routers {
		for _, route := range api.OrderedRoutes() {
			var handler http.Handler = route.HandlerFunc
{{#routers}}
	{{#mux}}
			handler = Logger(handler, route.Name)
			{{#featureCORS}}
			handler = handlers.CORS()(handler)
			{{/featureCORS}}

			router.
				Methods(route.Method).
				Path(route.Pattern).
				Name(route.Name).
				Handler(handler)
	{{/mux}}
	{{#chi}}
			router.Method(route.Method, route.Pattern, handler)
	{{/chi}}
{{/routers}}
		}
	}

	return router
}
